{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import sys\n",
    "sys.path.insert(1, '../exps/')\n",
    "import posgres_helper as db_helper\n",
    "from retrieving_botscore import *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Check botscore of friends/followers of bots"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "users_to_check_botscore = db_helper.getDataframeFromQuery(\n",
    "    db_helper.connect_db(), \n",
    "    \"\"\"\n",
    "    select \n",
    "        c.t_usr_id_ego, \n",
    "        c.t_usr_id_conn, \n",
    "        c.conn_type\n",
    "    from\n",
    "        bot b,\n",
    "        connections c\n",
    "    where b.twitter_user_id = c.t_usr_id_ego\n",
    "    ;\"\"\"\n",
    ")\n",
    "users_to_check_botscore.drop_duplicates(inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## saving bot scores to json files for later processing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "tw_keys = {\n",
    "    \"consumer_key\": \"<replace_here>\",\n",
    "    \"consumer_secret\": \"<replace_here>\",\n",
    "    \"access_token\": \"<replace_here>\",\n",
    "    \"access_token_secret\": \"<replace_here>\",\n",
    "    \"mashape_key\": \"<replace_here>\",\n",
    "}\n",
    "check_botscore = RetrieveBotscore(tw_keys)\n",
    "check_botscore.check_botometer_scores(users_to_check_botscore.t_usr_id_conn.unique())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Reading bot scores from json file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "json_file = \"<FILL_HERE_THE_NAME_OF_THE_FILE_SAVED_IN_THE_PREVIOUS_STEP>\"\n",
    "botscore_measurements = check_botscore.loadJson(json_file)[[\n",
    "    \"user_id_str\",\"user_screen_name\",\"scores_english\"\n",
    "]]\n",
    "botscore_measurements = botscore_measurements.join(\n",
    "    pd.DataFrame(users_to_check_botscore,columns=[\"user_id_str\"]).set_index(\"user_id_str\"),\n",
    "    on=\"user_id_str\",\n",
    "    how=\"outer\"\n",
    ")\n",
    "botscore_measurements = botscore_measurements[~botscore_measurements.user_id_str.isna()]\n",
    "botscore_measurements.set_index(\"user_id_str\", inplace=True)\n",
    "\n",
    "connections_overall_botscores = users_to_check_botscore.groupby(\n",
    "    [\"t_usr_id_ego\",\"conn_type\"]\n",
    ").t_usr_id_conn.apply(\n",
    "    lambda conn_ids: pd.Series(np.concatenate(conn_ids.values)).unique()\n",
    ").apply(\n",
    "    lambda x: [botscore_measurements.loc[str(x_i)].scores_english for x_i in x]\n",
    ").apply(pd.Series).transpose()\n",
    "connections_overall_botscores = connections_overall_botscores.rename(\n",
    "    columns={True:\"follower\",False:\"friend\"}\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "connections_overall_botscores.to_csv(\"../data/connections_botscores.csv\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "botscore_evolution-2bs2KYsp",
   "language": "python",
   "name": "botscore_evolution-2bs2kysp"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
